package com.bub.pay.core.manager.menu.impl;

import com.bub.pay.core.dao.SysPermDao;
import com.bub.pay.core.dao.SysRolePermDao;
import com.bub.pay.core.entity.domain.SysPerm;
import com.bub.pay.core.entity.domain.SysRolePerm;
import com.bub.pay.core.entity.vo.user.AddSysPermVo;
import com.bub.pay.core.entity.vo.user.UpdateRoleVo;
import com.bub.pay.core.base.BaseManagerAbstract;
import com.bub.pay.core.manager.menu.PermManager;
import com.bub.pay.core.manager.role.RoleManager;
import com.bub.pay.framework.common.base.Cause;
import com.bub.pay.core.manager.system.CurrentSystemManager;
import com.bub.pay.framework.common.exception.GlobalException;
import com.bub.pay.framework.common.util.CollectionUtil;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermManagerImpl extends BaseManagerAbstract<SysPerm, SysPermDao> implements PermManager {

    @Resource
    private SysPermDao sysPermDao;

    @Resource
    private SysRolePermDao sysRolePermDao;

    @Resource
    private CurrentSystemManager currentSystemManager;

    @Resource
    private RoleManager roleManager;

    @Override
    public int deletePerm(AddSysPermVo delVo) {
        // 删除角色权限关系表
        SysRolePerm sysRolePerm = new SysRolePerm();
        sysRolePerm.setDelFlag(true);
        Example rolePermExample = new Example(SysRolePerm.class);
        rolePermExample.createCriteria().andEqualTo("permId",delVo.getId());
        sysRolePermDao.updateByExampleSelective(sysRolePerm,rolePermExample);
        // 删除权限
        SysPerm sysPerm = new SysPerm();
        sysPerm.setDelFlag(true);
        Example perExa = new Example(SysPerm.class);
        perExa.createCriteria().orEqualTo("id",delVo.getId()).orEqualTo("parentId",delVo.getId());
        int i = sysPermDao.updateByExampleSelective(sysPerm,perExa);
        return i;
    }

    @Override
    public int updateRolePerm(UpdateRoleVo updateRoleVo) {
        // 删除角色权限关系表
        Example rolePermExample = new Example(SysRolePerm.class);
        rolePermExample.createCriteria().andEqualTo("roleId",updateRoleVo.getRoleId());
        SysRolePerm sysRolePerm = new SysRolePerm();
        sysRolePerm.setDelFlag(true);
        sysRolePermDao.updateByExampleSelective(sysRolePerm,rolePermExample);
        List<SysRolePerm> rpList = updateRoleVo.getPermIds().stream().map(pid ->{
            SysRolePerm perm = new SysRolePerm();
            perm.setRoleId(updateRoleVo.getRoleId());
            perm.setPermId(pid);
            return perm;
        }).collect(Collectors.toList());
        int i=0;
        if(!CollectionUtil.isNullOrEmpty(rpList)){
             i= sysRolePermDao.insertList(rpList);
            if(i==0){
                throw new GlobalException(currentSystemManager.getModule(), Cause.admin_add_role_menu_fail);
            }
        }
        roleManager.deleteRolePremCache(updateRoleVo.getRoleId());
        return i;
    }

    @Override
    public List<SysPerm> roleDifferencePerm(Long roleId) {
        return sysPermDao.roleDifferencePerm(roleId);
    }

    @Override
    public int countPermSizeByRole(Long roleId) {
        return sysPermDao.countPermSizeByRole(roleId);
    }

    @Override
    public int countEmployeeNumByRole(Long roleId) {
        return sysPermDao.countEmployeeNumByRole(roleId);
    }

}
